package com.restful.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class RestfulController {

    //传统风格 :  http://localhost:8080/springmvc_restful_example_war_exploded/add?a=1&b=2
    @RequestMapping("/add")
    public String add1(int a, int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果为 : " + res);
        return "test";
    }

    //restful风格 : http://localhost:8080/springmvc_restful_example_war_exploded/add/1/2
    @RequestMapping("/add/{p1}/{p2}")
    public String add2(@PathVariable int p1, @PathVariable int p2, Model model){
        int res = p1 + p2;
        model.addAttribute("msg","结果为: " + res);
        return "test";
    }

    @RequestMapping("/strAdd/{p1}/{p2}")
    public String add2(@PathVariable int p1, @PathVariable String p2, Model model){
        String res = p1 + p2;
        model.addAttribute("msg","结果为: " + res);
        return "test";
    }


    //http://localhost:8080/springmvc_restful_example_war_exploded/hello
    //HTTP状态 405 - 方法不允许,因为浏览器所有的地址栏请求默认都会是 HTTP GET 类型的。
    @PostMapping("/hello")
    public String hello1( Model model){
        model.addAttribute("msg","hello1!");
        return "test";
    }

    //如果再添加一个get类型的映射,则能正常访问,且走的是get
    @GetMapping("/hello")
    public String hello2( Model model){
        model.addAttribute("msg","hello2!");
        return "test";
    }





}
